﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LUStationery.Controller;
using LUStationery.Model;

public partial class Ordering : System.Web.UI.Page
{
    SupplierControl suppliercont;
    CategoryListControl categorycont;
    ItemListControl itemcont;
    OrdersControl orderscont;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            suppliercont = new SupplierControl();
            ddlSupplier.DataSource = suppliercont.getSuppliers();
            ddlSupplier.DataTextField = "SupplierName";
            ddlSupplier.DataValueField = "SupplierName";
            ddlSupplier.DataBind();

            categorycont = new CategoryListControl();
            ddlCategory.DataSource = categorycont.getCategories();
            ddlCategory.DataTextField = "CategoryDesc";
            ddlCategory.DataValueField = "CategoryDesc";
            ddlCategory.DataBind();

            itemcont = new ItemListControl();
            string supplier = ddlSupplier.SelectedValue;
            string category = ddlCategory.SelectedValue;
            gvInventory.DataSource = itemcont.getItemsBySupplierAndCategory(supplier,category);
            gvInventory.DataBind();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        itemcont = new ItemListControl();
        string supplier = ddlSupplier.SelectedValue;
        string category = ddlCategory.SelectedValue;
        gvInventory.DataSource = itemcont.getItemsBySupplierAndCategory(supplier,category);
        gvInventory.DataBind();
    }
    protected void btnAddOrderRequest_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvInventory.Rows)
        {
            TextBox txt = (TextBox)row.FindControl("txtReorderQty");
            if (txt.Text.Length>0)
            {
                string supplier = ddlSupplier.SelectedValue;
                string itemCode = row.Cells[1].Text.ToString();

                int orderQty = int.Parse(txt.Text);
                orderscont = new OrdersControl();
                orderscont.addOrder(supplier, itemCode, orderQty);
                Response.Write("<script>alert('Add Order Request successful!')</script>)");

            }
        }
    }
    protected void btnUnsafeStock_Click(object sender, EventArgs e)
    {
        itemcont = new ItemListControl();
        gvInventory.DataSource = itemcont.getUnSafeStockItems();
        gvInventory.DataBind();
    }
}
